package com.example.rbacmanage.service.impl;

import com.example.rbacmanage.common.ScheduleConstants;
import com.example.rbacmanage.entity.SysJob;
import com.example.rbacmanage.exception.MyException;
import com.example.rbacmanage.exception.TaskException;
import com.example.rbacmanage.mapper.SysJobMapper;
import com.example.rbacmanage.service.SysJobService;
import com.example.rbacmanage.util.ScheduleUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.quartz.JobDataMap;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class SysJobServiceImpl implements SysJobService {
    @Autowired
    private SysJobMapper sysJobMapper;
    @Autowired
    private Scheduler scheduler;
    @Override
    public void save(SysJob sysJob){
        sysJob.setCreateTime(new Date());
        sysJobMapper.insert(sysJob);
    }

    @Override
    public void remove(Long[] ids){
        for (Long id : ids) {
            sysJobMapper.del(id);
        }
    }

    @Override
    public void update(SysJob sysJob){
        sysJobMapper.update(sysJob);
    }

    @Override
    public List<SysJob> getAll(SysJob sysJob) {
        return sysJobMapper.list(sysJob);
    }

    @Override
    public PageInfo<SysJob> getPage(SysJob sysJob) {
        PageHelper.startPage(sysJob.getPage(),sysJob.getLimit());
        return new PageInfo<>(getAll(sysJob));
    }

    @Override
    public SysJob getById(Long id) {
        return sysJobMapper.queryOneById(id);
    }

    @Override
    public Integer getCount(SysJob sysJob) {
        return sysJobMapper.count(sysJob);
    }

    @Override
    public void checkRepeat(SysJob sysJob) throws MyException {

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean run(SysJob job)  throws SchedulerException {
        boolean result = false;
        Long jobId = job.getJobId();
        String jobGroup = job.getJobGroup();
        SysJob properties = getById(job.getJobId());
        // 参数
        JobDataMap dataMap = new JobDataMap();
        dataMap.put(ScheduleConstants.TASK_PROPERTIES, properties);
        JobKey jobKey = ScheduleUtils.getJobKey(jobId, jobGroup);
        if (scheduler.checkExists(jobKey))
        {
            result = true;
            scheduler.triggerJob(jobKey, dataMap);
        }
        return result;
    }

    @Override
    public void changeStatus(SysJob newJob) throws SchedulerException {
        String status = newJob.getStatus();
        Long jobId = newJob.getJobId();
        String jobGroup = newJob.getJobGroup();
        if (ScheduleConstants.Status.NORMAL.getValue().equals(status)){
            newJob.setStatus(ScheduleConstants.Status.NORMAL.getValue());
            sysJobMapper.update(newJob);
            scheduler.resumeJob(ScheduleUtils.getJobKey(jobId,jobGroup));
        }else if (ScheduleConstants.Status.PAUSE.getValue().equals(status)){
            newJob.setStatus(ScheduleConstants.Status.PAUSE.getValue());
            sysJobMapper.update(newJob);
            scheduler.pauseJob(ScheduleUtils.getJobKey(jobId,jobGroup));
        }
    }

    @Override
    public void insertJob(SysJob job) throws SchedulerException, TaskException {
        job.setCreateTime(new Date());
        sysJobMapper.insert(job);
        ScheduleUtils.createScheduleJob(scheduler,job);
    }

    @Override
    public void updateJob(SysJob sysJob) throws SchedulerException, TaskException {
        sysJobMapper.update(sysJob);
        Long jobId=sysJob.getJobId();
        String jobGroup=sysJob.getJobGroup();
        JobKey jobKey=ScheduleUtils.getJobKey(jobId,jobGroup);
        if (scheduler.checkExists(jobKey)){
            scheduler.deleteJob(jobKey);
        }
        ScheduleUtils.createScheduleJob(scheduler,sysJob);
    }

    @Override
    public void removeJob(Long[] jobIds) throws SchedulerException {
        for (Long id : jobIds) {
            SysJob job= sysJobMapper.queryOneById(id);
            Long jobId=job.getJobId();
            String jobGroup=job.getJobGroup();
            sysJobMapper.del(id);
            scheduler.deleteJob(ScheduleUtils.getJobKey(jobId,jobGroup));
        }
    }
}
